package com.bing.bingPro.service.impl;

import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bing.bingPro.controller.dto.ZdBasicOrderDto;
import com.bing.bingPro.controller.vo.ZdBasicInfoVo;
import com.bing.bingPro.entity.DZoneInfo;
import com.bing.bingPro.entity.ZdBasicInfo;
import com.bing.bingPro.mapper.DZoneInfoMapper;
import com.bing.bingPro.mapper.ZdBasicInfoMapper;
import com.bing.bingPro.service.ZdBasicInfoService;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author 29821
 * @description 针对表【zd_basic_info】的数据库操作Service实现
 * @createDate 2023-08-10 15:52:09
 */
@Service
public class ZdBasicInfoServiceImpl extends ServiceImpl<ZdBasicInfoMapper, ZdBasicInfo>
        implements ZdBasicInfoService {

    @Resource
    private DZoneInfoMapper dZoneInfoMapper;


    @Override
    public List<ZdBasicInfoVo> zdBasicList(ZdBasicOrderDto zdBasicOrderDto) {
        String time = zdBasicOrderDto.getTime();
        String province = getKeyProvince(zdBasicOrderDto.getProvince());

        List<ZdBasicInfo> zeds = baseMapper.selectList(Wrappers.lambdaQuery(ZdBasicInfo.class)
                .eq(CharSequenceUtil.isNotEmpty(zdBasicOrderDto.getProvince()), ZdBasicInfo::getProvinceCode, province)
                .eq(CharSequenceUtil.isNotEmpty(time), ZdBasicInfo::getDt, time));


        List<DZoneInfo> dZoneInfos = dZoneInfoMapper.selectByZoneIds(zeds.stream()
                .filter(z -> CharSequenceUtil.isNotEmpty(z.getCityCode()))
                .filter(z -> !Objects.equals(z.getCityCode(), "null"))
                .map(z -> Long.parseLong(z.getCityCode()))
                .collect(Collectors.toList()));
        Map<Integer, DZoneInfo> zoneInfoMap = dZoneInfos.stream().collect(Collectors.toMap(DZoneInfo::getZoneId, Function.identity(),(existingValue, newValue) -> newValue));

        return zeds.stream().map(zdBasicInfo -> {
            ZdBasicInfoVo vo = new ZdBasicInfoVo();
            BeanUtils.copyProperties(zdBasicInfo, vo);
            vo.setProvinceCode(getProvince(zdBasicInfo.getProvinceCode()));

            if (CharSequenceUtil.isNotEmpty(zdBasicInfo.getCityCode())&&!Objects.equals(zdBasicInfo.getCityCode(), "null")) {
                Integer zoneId = Integer.parseInt(zdBasicInfo.getCityCode());
                DZoneInfo dZoneInfo = zoneInfoMap.get(zoneId);
                vo.setCityCode(dZoneInfo.getZoneName());
            }else {
                vo.setCityCode(null);
            }
            return vo;
        }).collect(Collectors.toList());
    }

    @SuppressWarnings("unchecked")
    @Override
    public IPage<ZdBasicInfoVo> zdBasicIPageList(ZdBasicOrderDto zdBasicOrderDto) {
        IPage<ZdBasicInfoVo> voIPage = new Page<>();
        Page page = new Page(zdBasicOrderDto.getPageNum(), zdBasicOrderDto.getPageSize());
        String time = zdBasicOrderDto.getTime();
        String province = getKeyProvince(zdBasicOrderDto.getProvince());

        IPage<ZdBasicInfo> zedPage = baseMapper.selectPage(page, Wrappers.lambdaQuery(ZdBasicInfo.class)
                .eq(CharSequenceUtil.isNotEmpty(zdBasicOrderDto.getProvince()), ZdBasicInfo::getProvinceCode, province)
                .eq(CharSequenceUtil.isNotEmpty(time), ZdBasicInfo::getDt, time));
        List<ZdBasicInfo> zeds = zedPage.getRecords();

        List<DZoneInfo> dZoneInfos = dZoneInfoMapper.selectByZoneIds(zeds.stream()
                .filter(z -> CharSequenceUtil.isNotEmpty(z.getCityCode()))
                .map(z -> Long.parseLong(z.getCityCode()))
                .collect(Collectors.toList()));
        Map<Integer, DZoneInfo> zoneInfoMap = dZoneInfos.stream()
                .collect(Collectors.toMap(DZoneInfo::getZoneId, Function.identity(), (existingValue, newValue) -> newValue));

        List<ZdBasicInfoVo> collect = zeds.stream().map(zdBasicInfo -> {
            ZdBasicInfoVo vo = new ZdBasicInfoVo();
            BeanUtils.copyProperties(zdBasicInfo, vo);
            vo.setProvinceCode(getProvince(zdBasicInfo.getProvinceCode()));

            if (CharSequenceUtil.isNotEmpty(zdBasicInfo.getCityCode())) {
                Integer zoneId = Integer.parseInt(zdBasicInfo.getCityCode());
                DZoneInfo dZoneInfo = zoneInfoMap.get(zoneId);
                vo.setCityCode(dZoneInfo.getZoneName());
            }
            return vo;
        }).collect(Collectors.toList());
        voIPage.setRecords(collect);
        voIPage.setTotal(zedPage.getTotal());
        voIPage.setPages(zedPage.getPages());
        voIPage.setCurrent(zedPage.getCurrent());
        voIPage.setSize(zedPage.getSize());
        return voIPage;
    }

    @Override
    public List<String> getCountry() {
        List<String> country = baseMapper.getCountry();
        country.add(0, "全国");
        return country;
    }


    public static String getKeyProvince(String code) {
        final Map<String, String> provinceGetKeyMap = new LinkedHashMap<>();
        provinceGetKeyMap.put("安徽", "AH");
        provinceGetKeyMap.put("北京", "BJ");
        provinceGetKeyMap.put("重庆", "CQ");
        provinceGetKeyMap.put("福建", "FJ");
        provinceGetKeyMap.put("广东", "GD");
        provinceGetKeyMap.put("甘肃", "GS");
        provinceGetKeyMap.put("广西", "GX");
        provinceGetKeyMap.put("贵州", "GZ");
        provinceGetKeyMap.put("河南", "HA");
        provinceGetKeyMap.put("湖北", "HB");
        provinceGetKeyMap.put("河北", "HE");
        provinceGetKeyMap.put("海南", "HI");
        provinceGetKeyMap.put("黑龙江", "HL");
        provinceGetKeyMap.put("湖南", "HN");
        provinceGetKeyMap.put("吉林", "JL");
        provinceGetKeyMap.put("江苏", "JS");
        provinceGetKeyMap.put("江西", "JX");
        provinceGetKeyMap.put("辽宁", "LN");
        provinceGetKeyMap.put("内蒙古", "NM");
        provinceGetKeyMap.put("宁夏", "NX");
        provinceGetKeyMap.put("青海", "QH");
        provinceGetKeyMap.put("四川", "SC");
        provinceGetKeyMap.put("山东", "SD");
        provinceGetKeyMap.put("上海", "SH");
        provinceGetKeyMap.put("陕西", "SN");
        provinceGetKeyMap.put("山西", "SX");
        provinceGetKeyMap.put("天津", "TJ");
        provinceGetKeyMap.put("新疆", "XJ");
        provinceGetKeyMap.put("西藏", "XZ");
        provinceGetKeyMap.put("云南", "YN");
        provinceGetKeyMap.put("浙江", "ZJ");
        if (!provinceGetKeyMap.containsKey(code)) {
            return null;
        }
        return provinceGetKeyMap.get(code);
    }

    public static String getProvince(String code) {
        final Map<String, String> provinceNameMap = new LinkedHashMap<>();
        provinceNameMap.put("AH", "安徽");
        provinceNameMap.put("BJ", "北京");
        provinceNameMap.put("CQ", "重庆");
        provinceNameMap.put("FJ", "福建");
        provinceNameMap.put("GD", "广东");
        provinceNameMap.put("GS", "甘肃");
        provinceNameMap.put("GX", "广西");
        provinceNameMap.put("GZ", "贵州");
        provinceNameMap.put("HA", "河南");
        provinceNameMap.put("HB", "湖北");
        provinceNameMap.put("HE", "河北");
        provinceNameMap.put("HI", "海南");
        provinceNameMap.put("HL", "黑龙江");
        provinceNameMap.put("HN", "湖南");
        provinceNameMap.put("JL", "吉林");
        provinceNameMap.put("JS", "江苏");
        provinceNameMap.put("JX", "江西");
        provinceNameMap.put("LN", "辽宁");
        provinceNameMap.put("NM", "内蒙古");
        provinceNameMap.put("NX", "宁夏");
        provinceNameMap.put("QH", "青海");
        provinceNameMap.put("SC", "四川");
        provinceNameMap.put("SD", "山东");
        provinceNameMap.put("SH", "上海");
        provinceNameMap.put("SN", "陕西");
        provinceNameMap.put("SX", "山西");
        provinceNameMap.put("TJ", "天津");
        provinceNameMap.put("XJ", "新疆");
        provinceNameMap.put("XZ", "西藏");
        provinceNameMap.put("YN", "云南");
        provinceNameMap.put("ZJ", "浙江");
        if (!provinceNameMap.containsKey(code)) {
            return null;
        }
        return provinceNameMap.get(code);
    }
}




